<?php

namespace App\Admin\Controllers;

use App\Models\Tool\Share;
use App\Models\WechatMenu;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;

class ShareController extends AdminController
{
    public function index(Content $content)
    {
        $tree = new Tree(new Share());
        return $content
            ->header('分享管理')
            ->body($tree);
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Share::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('icon', __('图标'));
        $show->field('title', __('模块名称'));
        $show->field('value', __('模块内容'));
        $show->field('sort', __('排序'));
        $show->field('type', __('跳转类型'));
        $show->field('points', __('积分'));
        $show->field('is_free', __('是否免费'));
        $show->field('remark', __('备注'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Share());
        $form->display('id', __('ID'));
        $form->display('pid', __('父级ID'))->customFormat(function ($value) {
            if ($value == 0) {
                return "顶级菜单";
            }
            return Share::query()->where('id', $value)->value('title');
        })->disable();
        $form->text('icon', __('图标'));
        $form->text('title', __('模块名称'));
        $form->text('value', __('模块内容'));
        $form->select('type', __('类型'))->options(function ($type) {
            if ($this->pid == 0) {
                return ['view' => '查看详情'];
            }
            return ['copy' => '复制', 'jump' => '跳转链接'];
        })->default('copy');
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
        ];

        $free = [
            'on'  => ['value' => 1, 'text' => '免费', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '收费', 'color' => 'danger'],
        ];

        $form->switch('is_free', __('是否免费'))->options($free);
        $form->number('points', __('积分'))->value(5);

        $form->switch('state', __('状态'))->options($states);
        $form->text('sort', __('排序'));
        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`提交`按钮
//            $footer->disableSubmit();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        return $form;
    }
}
